﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace EnvRide.Core.ObjectModel
{
    /// <summary>
    /// Represents the inclusion of another AttributeOverride.
    /// </summary>
    public class AttributeOverrideInclude : OverrideInclude
    {
        protected override Override GetOverride(String overrideName, Environment env)
        {
            if (EnvRideProject.Logger.IsDebugEnabled)
                EnvRideProject.Logger.DebugFormat("Looking for attribute override \"{0}\"..", overrideName);

            Override over = null;

            if (env.AttributeOverrides != null && env.AttributeOverrides.Count > 0)
            {
                over = env.AttributeOverrides.SingleOrDefault(inOver => inOver.Name.Equals(overrideName, StringComparison.CurrentCultureIgnoreCase));
            }

            return over;
        }

        protected override void AddAllOverridesInternal(Environment env, EnvRideProcessArguments args)
        {
            env.ProcessAttributeOverrides(args);
        }

        public override string ToString()
        {
            String name = null;

            if (Environment != null)
            {
                name = String.Format("AttributeOverrideInclude[{0}]", Environment.IncludedAttributeOverrides.IndexOf(this));
            }
            else if (Project != null)
            {
                name = String.Format("AttributeOverrideInclude[{0}]", Project.IncludedAttributeOverrides.IndexOf(this));
            }
            else
            {
                name = "AttributeOverrideInclude";
            }

            return name;
        }
    }
}
